﻿using UnityEngine;
using System.Collections;

namespace MyAssetBundles {
    public struct ProgressTarget {
        public float progress;
        public long totalNum;
        public string fileNameCurr;

        public long processNum
        {
            get {
                return (long)(totalNum * progress);
            }
        }
    }

    public interface IFolderProgress {
        void SetProgress ();
    }

    public class FolderProgress : IFolderProgress {
        public delegate void ProgressHandler (ProgressTarget target);
        public event ProgressHandler onEventProgress;
        public ProgressTarget target = new ProgressTarget();
        public static FolderProgress Create (ProgressHandler handler = null)
        {
            return Create<FolderProgress>(handler);
        }

        public static T Create<T> (ProgressHandler handler = null) where T : FolderProgress, new()
        {
            T t = new T();
            t.onEventProgress += handler;
            return t;
        }

        public virtual void SetProgress ()
        {
            if (onEventProgress != null)
                onEventProgress(target);
            //   Debug.Log(string.Format("process number: {0}, total number: {1}", processByte, totalByte));
            ShowProgress();
        }

        protected virtual void ShowProgress () { }
        protected virtual void ShowProgress (string title, string info)
        {
#if UNITY_EDITOR
            UnityEditor.EditorUtility.DisplayProgressBar(title, info, target.progress);
#endif
        }

        public static void ClearProgress ()
        {
#if UNITY_EDITOR
            UnityEditor.EditorUtility.ClearProgressBar();
#endif
        }

    }
}

